﻿// <copyright file="CurrencyPair.cs" company="Golizza.net">Copyright (c) 2012 All Right Reserved</copyright>

namespace Sharemarker.BinaryOptionApplication.Models
{
    using System;
    using System.Collections.ObjectModel;

    using Sharemarker.Common.Framework;

    /// <summary>
    /// Model for the candle chart series.
    /// </summary>
    public class CurrencyPair : NotifyPropertyChangedBase
    {
        #region Fields

        /// <summary>
        /// The back up value for <see cref="CandleCollection"/> property.
        /// </summary>
        private ObservableCollection<Tick> candleCollection;

        /// <summary>
        /// The back up value for <see cref="FinishTime"/> property.
        /// </summary>
        private DateTime finishTime;

        /// <summary>
        /// The back up value for <see cref="FirstCurrency"/> property.
        /// </summary>
        private string firstCurrency;

        /// <summary>
        /// The back up value for <see cref="MaxValue"/> property.
        /// </summary>
        private double maxValue;

        /// <summary>
        /// The back up value for <see cref="MinValue"/> property.
        /// </summary>
        private double minValue;

        /// <summary>
        /// The back up value for <see cref="SecondCurrency"/> property.
        /// </summary>
        private string secondCurrency;

        /// <summary>
        /// The back up value for <see cref="StartTime"/> property.
        /// </summary>
        private DateTime startTime;

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets the candle collection.
        /// </summary>
        /// <value>
        /// The candle collection.
        /// </value>
        public ObservableCollection<Tick> CandleCollection
        {
            get
            {
                return this.candleCollection ?? (this.candleCollection = new ObservableCollection<Tick>());
            }
        }

        /// <summary>
        /// Gets or sets the FinishTime.
        /// </summary>
        public DateTime FinishTime
        {
            get
            {
                return this.finishTime;
            }

            set
            {
                if (this.finishTime != value)
                {
                    this.finishTime = value;
                    this.OnPropertyChanged("FinishTime");
                }
            }
        }

        /// <summary>
        /// Gets or sets the first currency.
        /// </summary>
        /// <value>
        /// The first currency.
        /// </value>
        public string FirstCurrency
        {
            get
            {
                return this.firstCurrency;
            }

            set
            {
                if (this.firstCurrency != value)
                {
                    this.firstCurrency = value;
                    this.OnPropertyChanged("FirstCurrency");
                }
            }
        }

        /// <summary>
        /// Gets the max value.
        /// </summary>
        /// <value>
        /// The max value.
        /// </value>
        public double MaxValue
        {
            get
            {
                return this.maxValue;
            }

            private set
            {
                if (this.maxValue != value)
                {
                    this.maxValue = value;
                    this.OnPropertyChanged("MaxValue");
                }
            }
        }

        /// <summary>
        /// Gets the min value.
        /// </summary>
        /// <value>
        /// The min value.
        /// </value>
        public double MinValue
        {
            get
            {
                return this.minValue;
            }

            private set
            {
                if (this.minValue != value)
                {
                    this.minValue = value;
                    this.OnPropertyChanged("MinValue");
                }
            }
        }

        /// <summary>
        /// Gets or sets the second currency.
        /// </summary>
        /// <value>
        /// The second currency.
        /// </value>
        public string SecondCurrency
        {
            get
            {
                return this.secondCurrency;
            }

            set
            {
                if (this.secondCurrency != value)
                {
                    this.secondCurrency = value;
                    this.OnPropertyChanged("SecondCurrency");
                }
            }
        }

        /// <summary>
        /// Gets or sets the StartTime.
        /// </summary>
        public DateTime StartTime
        {
            get
            {
                return this.startTime;
            }

            set
            {
                if (this.startTime != value)
                {
                    this.startTime = value;
                    this.OnPropertyChanged("StartTime");
                }
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// Adds the specified value.
        /// </summary>
        /// <param name="candle">The value.</param>
        public void Add(Tick candle)
        {
            if (this.CandleCollection.Count == 0)
            {
                this.StartTime = candle.TimeStamp;
            }

            this.FinishTime = candle.TimeStamp;

            this.CandleCollection.Add(candle);

            this.MinValue = Math.Min(
                this.MinValue, Math.Min(candle.Low, Math.Min(candle.High, Math.Min(candle.Close, candle.Open))));
            this.MaxValue = Math.Max(
                this.MaxValue, Math.Max(candle.Low, Math.Max(candle.High, Math.Max(candle.Close, candle.Open))));
        }

        #endregion
    }
}